package com.zhihaoliang.room

import android.content.Context
import com.zhihaoliang.room.data.*
import com.zhihaoliang.room.data.relationships.Library
import com.zhihaoliang.room.data.relationships.Playlist
import com.zhihaoliang.room.data.relationships.UserLibrary
import com.zhihaoliang.room.data.relationships.UserPlay
import com.zhihaoliang.room.util.log
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch

/**
 * @author 支豪亮
 * @date 2023/1/30 16:01
 */
class MainPresenter(private val mainScope : CoroutineScope, context: Context) {
    private val plantDao = AppDatabase.getInstance(context).plantDao()
    private val userDao = AppDatabase.getInstance(context).userDao()
    private val userFtsDao = AppDatabase.getInstance(context).userFtsDao()
    private val userIndexDao = AppDatabase.getInstance(context).userIndexDao()
    private val userAdDao = AppDatabase.getInstance(context).userAdDao()
    private val userLibraryDao = AppDatabase.getInstance(context).userLibraryDao()
    private val oneToManyDao = AppDatabase.getInstance(context).oneToManyDao()

    private val list = listOf(
        UserFts(6, "张", "三"),
        UserFts(1, "李", "四"),
        UserFts(2, "王", "五"),
        UserFts(3, "赵", "六"),
        UserFts(4, "赵", "一"),
        UserFts(5, "孙", "二娘"),
    )

    fun getPlants(){
        mainScope.launch {
            log(plantDao.getPlants())
        }
    }

    fun insertUser(){
        mainScope.launch {
            val users = List(50000){
                val user = list[it%list.size]
                User((it+1), "$it${user.lastName}", "${user.firstName}")
            }
            val longTime = System.currentTimeMillis()
            userDao.insertAll(users)
            log("插入 ：${System.currentTimeMillis() - longTime}")
        }
    }

    fun insertFtsUsers(){
        mainScope.launch {
            val users = List(50000){
                val user = list[it%list.size]
                UserFts((it+1), "$it${user.lastName}", "${user.firstName}")
            }
            val longTime = System.currentTimeMillis()
            userFtsDao.insertAll(users)
            log("Fts4插入 ：${System.currentTimeMillis() - longTime}")
        }
    }

    fun insertIndexUsers(){
        mainScope.launch {
            val users = List(50000){
                val user = list[it%list.size]
                UserIndex((it+1), "$it${user.lastName}", "${user.firstName}")
            }
            val longTime = System.currentTimeMillis()
            userIndexDao.insertAll(users)
            log("Fts4插入 ：${System.currentTimeMillis() - longTime}")
        }
    }

    fun queryLikeUsers(){
        mainScope.launch {
            val longTime = System.currentTimeMillis()
            val list = userDao.queryLikeUsers()
            log("Like ：${System.currentTimeMillis() - longTime}: ${list.size}")
        }
    }

    fun queryFtsMatchUsers(){
        mainScope.launch {
            val longTime = System.currentTimeMillis()
            val list = userFtsDao.queryMatchUsers()
            log("Fts4 Match ：${System.currentTimeMillis() - longTime}: ${list.size}")
        }
    }

    fun queryFtsLikeUsers(){
        mainScope.launch {
            val longTime = System.currentTimeMillis()
            val list = userFtsDao.queryLikeUsers()
            log("Fts4 Like ：${System.currentTimeMillis() - longTime}: ${list.size}")
        }
    }

    fun queryIndexLikeUsers(){
        mainScope.launch {
            val longTime = System.currentTimeMillis()
            val list = userIndexDao.queryLikeUsers()
            log("Index Like ：${System.currentTimeMillis() - longTime}: ${list.size}")
        }
    }

    var index = 500000
    fun insertIndexUser() {
        mainScope.launch {
            try {
                val userIndex = UserIndex(index, "大山", "支")
                log(userIndex.toString())
                userIndexDao.insert(userIndex)
                index++
            } catch (e: Exception) {
                log("insertIndexUser ：$e")
            }
        }
    }

    fun insertUserAd(){
        mainScope.launch {
            userAdDao.insertAll(listOf(
                UserAd("李四",Address("学院路","20号","石家庄",10021))
            ))
        }
    }

    fun insertUserLibrary(){
        mainScope.launch {
            val users = listOf(
                UserLibrary(1,"张三",12),
                UserLibrary(2,"李四",13),
            )

            val library = listOf(
                Library(1,users[0].userId,"范佩西"),
                Library(2,users[1].userId,"那个夏天")
            )

            userLibraryDao.insertUser(users)
            userLibraryDao.insertLibrary(library)

            log("insertUserLibrary 数据插入完成")
        }
    }

    fun queryUsersAndLibraries(){
        mainScope.launch {
            val list = userLibraryDao.queryUsersAndLibraries()
            log("queryUsersAndLibraries $list")
        }
    }

    fun insertUserPlayList(){
        mainScope.launch {
            val users = listOf(
                UserPlay(1,"张三",12),
                UserPlay(2,"李四",13),
            )

            val library = listOf(
                Playlist(1,users[0].userId,"沧海一声笑"),
                Playlist(2,users[1].userId,"爱上一个不回家的人"),
                Playlist(3,users[0].userId,"花街"),
                Playlist(4,users[1].userId,"女人花")
            )
            oneToManyDao.insertUserPlay(users)
            oneToManyDao.insertPlayList(library)

            log("insertUserPlayList 数据插入完成")
        }
    }

    fun queryUsersWithPlaylists(){
        mainScope.launch {
            val list = oneToManyDao.queryUsersWithPlaylists()
            log("queryUsersWithPlaylists $list")
        }
    }

}